package com.cy.pj.modules.controller;

import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.common.web.domain.BaseController;
import com.cy.pj.modules.entity.SpuAttribute;
import com.cy.pj.modules.service.ISpuAttributeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 商品参数Controller
 *
 * @author kiven
 * @date 2023-02-09
 */
@Controller
@RequestMapping("/attribute")
public class SpuAttributeController extends BaseController
{
    private String prefix = "modules/attribute";

    @Autowired
    private ISpuAttributeService spuAttributeService;

    @RequiresPermissions("attribute:view")
    @GetMapping()
    public String attribute()
    {
        return prefix + "/attribute";
    }

    /**
     * 查询商品参数列表
     */
    @RequiresPermissions("attribute:list")
    @GetMapping("/list")
    @ResponseBody
    public JsonResult list(SpuAttribute spuAttribute,Integer pageCurrent)
    {
        return new JsonResult(spuAttributeService.selectSpuAttributeList(spuAttribute,pageCurrent));
    }

    ///**
    // * 导出商品参数列表
    // */
    //@RequiresPermissions("pj:attribute:export")
    //@PostMapping("/export")
    //@ResponseBody
    //public AjaxResult export(SpuAttribute spuAttribute)
    //{
    //    List<SpuAttribute> list = spuAttributeService.selectSpuAttributeList(spuAttribute);
    //    ExcelUtil<SpuAttribute> util = new ExcelUtil<SpuAttribute>(SpuAttribute.class);
    //    return util.exportExcel(list, "attribute");
    //}

    /**
     * 新增商品参数
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品参数
     */
    @RequiresPermissions("attribute:add")
    @PostMapping("/add")
    @ResponseBody
    public JsonResult addSave(SpuAttribute spuAttribute)
    {
        return new JsonResult(spuAttributeService.insertSpuAttribute(spuAttribute));
    }

    /**
     * 修改商品参数
     */
    @GetMapping("/edit/")
    public String edit( Long id, ModelMap mmap)
    {
        SpuAttribute spuAttribute = spuAttributeService.selectSpuAttributeById(id);
        mmap.put("spuAttribute", spuAttribute);
        return prefix + "/attribute_edit";
    }

    @GetMapping("/id")
    @ResponseBody
    public JsonResult getAttributeById(Long id){
        return new JsonResult(spuAttributeService.selectSpuAttributeById(id));
    }

    /**
     * 修改保存商品参数
     */
    @RequiresPermissions("attribute:update")
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult editSave(SpuAttribute spuAttribute)
    {
        return new JsonResult(spuAttributeService.updateSpuAttribute(spuAttribute));
    }

    /**
     * 删除商品参数
     */
    @RequiresPermissions("attribute:delete")
    @PostMapping( "/remove")
    @ResponseBody
    public JsonResult remove(String ids)
    {
        return new JsonResult(spuAttributeService.deleteSpuAttributeByIds(ids));
    }
}
